﻿using System;
using System.Runtime.InteropServices;

namespace Mrsoft.Model
{
    public struct XY
    {
        public XY(float x, float y)
        {
            X = x;
            Y = y;
        }

        public float X;
        public float Y;

        public float HowFar(float x, float y)
        {
            return HowFar(new XY(x, y));
        }

        public float HowFar(XY tem)
        {

            if (!tem.Islegal() || !Islegal())
                return 65535;
            var x = this.X - tem.X;
            var y = this.Y - tem.Y;
            return (float)Math.Sqrt(x * x + y * y);
        }

        public static bool operator ==(XY a, XY b)
        {
            return (Math.Abs(a.X - b.X) < 0.1f && Math.Abs(a.Y - b.Y) < 0.1f);
        }

        public static bool operator !=(XY a, XY b)
        {
            return !(Math.Abs(a.X - b.X) < 0.1f && Math.Abs(a.Y - b.Y) < 0.1f);
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public bool Islegal()
        {
            return (X > 1 && Y > 1);
        }


        public double Atan(XY p)
        {
            var yd = (-1) * (p.Y - this.Y);
            var xd = p.X - this.X;
            if (xd != 0)
            {
                var t = (Math.Atan(yd / xd) % Math.PI) / Math.PI;
                if (yd < 0)
                {
                    t += 1;
                }
                return t;
            }
            return xd > 0 ? 0.5 : 1.5;
        }
    }
}
